package com.tyj.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.tyj.common.exception.BizCodeEnume;
import com.tyj.gulimall.member.excepiton.PhoneExistException;
import com.tyj.gulimall.member.excepiton.RigsterUserException;
import com.tyj.gulimall.member.excepiton.UserNameExistException;
import com.tyj.gulimall.member.vo.SocialUser;
import com.tyj.gulimall.member.vo.UserLoginVo;
import com.tyj.gulimall.member.vo.UserRegistVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.tyj.gulimall.member.entity.MemberEntity;
import com.tyj.gulimall.member.service.MemberService;
import com.tyj.common.utils.PageUtils;
import com.tyj.common.utils.R;



/**
 * 会员
 *
 * @author tanyoujie
 * @email 983629093@qq.com
 * @date 2021-02-09 22:27:24
 */
@RestController
@RequestMapping("member/member")
@Slf4j
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 社交用户登录
     * @param socialUser
     * @return
     */
    @PostMapping("/oauth2/login")
    public R login(@RequestBody SocialUser socialUser) throws Exception {
        MemberEntity memberEntity=memberService.login(socialUser);
        if (memberEntity!=null){
            //效验成功返回正常数据
            return R.ok().setData(memberEntity);
        }else {
            //返回异常数据
            return R.error(BizCodeEnume.USER_LOGIN_EXCEPTION.getCode(),BizCodeEnume.USER_LOGIN_EXCEPTION.getMsg());
        }
    }

    /**
     * 普通用户登录
     */
    @PostMapping("/login")
    public R login(@RequestBody UserLoginVo vo){
          MemberEntity memberEntity=memberService.login(vo);
        log.debug("返回session数据="+memberEntity);

        if (memberEntity!=null){
              //效验成功返回正常数据
              return R.ok().setData(memberEntity);
          }else {
              //返回异常数据
              return R.error(BizCodeEnume.USER_LOGIN_EXCEPTION.getCode(),BizCodeEnume.USER_LOGIN_EXCEPTION.getMsg());
          }
    }

    /**
     * 注册用户
     */
    @PostMapping("/regist")
    public R regist(@RequestBody UserRegistVo vo){
        try {
            memberService.regist(vo);
            return R.ok();
        }catch (PhoneExistException e) {
            return R.error(BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());
        }catch (UserNameExistException b) {
            return R.error(BizCodeEnume.USERNAME_EXIST_EXCEPTION.getCode(),BizCodeEnume.USERNAME_EXIST_EXCEPTION.getMsg());
        }catch (RigsterUserException c) {
            return R.error(BizCodeEnume.UNKNOW_EXCEPTION.getCode(),BizCodeEnume.UNKNOW_EXCEPTION.getMsg());
        }
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
